<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('hotel.hotel.name')" type="string" v-model="baTable.form.items!.name"
                              prop="name" :placeholder="t('Please input field', { field: t('hotel.hotel.name') })"/>
                    <FormItem :label="t('hotel.hotel.member_id')" type="remoteSelect"
                              v-model="baTable.form.items!.member_id" prop="member_id"
                              :input-attr="{ pk: 'ba_member.id', field: 'nickname', remoteUrl: '/admin/Member/index?is_hotel=1' }"
                              :placeholder="t('Please select field', { field: t('hotel.hotel.member_id') })"/>
                    <FormItem :label="t('hotel.hotel.hotel_cate_ids')" type="remoteSelects"
                              v-model="baTable.form.items!.hotel_cate_ids" prop="hotel_cate_ids"
                              :input-attr="{ pk: 'ba_hotel_cate.id', field: 'name', remoteUrl: '/admin/hotel.Cate/index' }"
                              :placeholder="t('Please select field', { field: t('hotel.hotel.hotel_cate_ids') })"/>

                    <FormItem label="所属会员卡" type="remoteSelects"
                              v-model="baTable.form.items!.vip_card_ids" prop="vip_card_ids"
                              :input-attr="{ pk: 'ba_vip_card.id', field: 'name', remoteUrl: '/admin/vip.card/index?type=2' }"
                              placeholder="请选择所属会员卡"/>
<!--                    <FormItem :label="t('hotel.hotel.phone')" type="string" v-model="baTable.form.items!.phone"-->
<!--                              prop="phone" :placeholder="t('Please input field', { field: t('hotel.hotel.phone') })"/>-->

                    <FormItem label="房型" type="string" v-model="baTable.form.items!.layout"
                              prop="layout" placeholder="房型"/>

                    <FormItem label="床尺寸" type="string" v-model="baTable.form.items!.size"
                              prop="size" placeholder="床尺寸"/>
                    <FormItem :label="t('hotel.hotel.cover')" type="image" v-model="baTable.form.items!.cover"
                              prop="cover"/>
                    <FormItem :label="t('hotel.hotel.banner')" type="images" v-model="baTable.form.items!.banner"
                              prop="banner"/>
                    <FormItem label="所属城市" type="city" :input-attr="{level:2}" v-model="baTable.form.items!.city" prop="city"/>

                    <el-form-item label="地址"  prop="address">
                        <el-input
                            type="string"
                            v-model="baTable.form.items!.address"

                            placeholder="请选择地址"
                            readonly
                        >
                            <template #append>
                                <el-button @click="openMapPickPop">选择地点</el-button>
                            </template>
                        </el-input>
                    </el-form-item>


                    <FormItem :label="t('hotel.hotel.longitude')"  type="string" v-model="baTable.form.items!.longitude"
                              prop="longitude"
                              :placeholder="t('Please input field', { field: t('hotel.hotel.longitude') })"/>
                    <FormItem :label="t('hotel.hotel.latitude')"  type="string" v-model="baTable.form.items!.latitude"
                              prop="latitude"
                              :placeholder="t('Please input field', { field: t('hotel.hotel.latitude') })"/>
                    <FormItem :label="t('hotel.hotel.hotel_des')" type="editor" v-model="baTable.form.items!.hotel_des"
                              prop="hotel_des" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                              :placeholder="t('Please input field', { field: t('hotel.hotel.hotel_des') })"/>
                    <FormItem :label="t('hotel.hotel.traffic_bus')" type="editor"
                              v-model="baTable.form.items!.traffic_bus" prop="traffic_bus" @keyup.enter.stop=""
                              @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                              :placeholder="t('Please input field', { field: t('hotel.hotel.traffic_bus') })"/>
                    <FormItem :label="t('hotel.hotel.book_notice')" type="editor"
                              v-model="baTable.form.items!.book_notice" prop="book_notice" @keyup.enter.stop=""
                              @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                              :placeholder="t('Please input field', { field: t('hotel.hotel.book_notice') })"/>

                    <FormItem :label="t('排序')" type="number" prop="weigh" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.weigh" :placeholder="t('Please input field', { field: t('排序') })" />


                    <FormItem :label="t('hotel.hotel.status')" type="switch" v-model="baTable.form.items!.status"
                              prop="status"
                              :input-attr="{ content: { '0': t('hotel.hotel.status 0'), '1': t('hotel.hotel.status 1') } }"/>

                    <FormItem label="备注" type="textarea" v-model="baTable.form.items!.notes"
                              :input-attr="{ showWordLimit: false,  rows: 6 }"/>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                           type="primary">
                    {{
                        baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save')
                    }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type {FormInstance, FormItemRule} from 'element-plus'
import {inject, reactive, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import {useConfig} from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import {buildValidatorData} from '/@/utils/validate'
import {openTencentMapPickPop} from "/@/components/xmmap/tencentMap";
import {getTencentmapKey} from "/@/api/backend/xmmap/tencentmap";

import {ElNotification} from 'element-plus'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const {t} = useI18n()

const state = reactive({
    tencentMapKey: '',
})

// 获取腾讯地图key，可替换自己的接口
getTencentmapKey().then((res) => {
    state.tencentMapKey = res.data
})
const openMapPickPop = () => {
    if (state.tencentMapKey == '') {
        ElNotification({
            type: 'error',
            message: '请前往【系统配置】【第三方密钥】配置腾讯地图KEY',
        })
        return false
    }
    openTencentMapPickPop(state.tencentMapKey)
}
window.addEventListener('message', function (event) {
    if (event.data.type == 'tencent-map-data') {
        const currentData = JSON.parse(event.data.data)
        console.log('腾讯地图当前位置数据：', currentData)
        baTable.form.items!.poiname = currentData.poiname
        baTable.form.items!.address = currentData.poiaddress
        baTable.form.items!.longitude = currentData.latlng.lng
        baTable.form.items!.latitude = currentData.latlng.lat
    }
})

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    name: [buildValidatorData({name: 'required', title: t('hotel.hotel.name')})],
    hotel_cate_ids: [buildValidatorData({name: 'required', title: t('hotel.hotel.hotel_cate_ids')})],
    vip_card_ids: [buildValidatorData({name: 'required', title: "会员卡"})],
    phone: [buildValidatorData({name: 'required', title: t('hotel.hotel.phone')})],
    cover: [buildValidatorData({name: 'required', title: t('hotel.hotel.cover')})],
    banner: [buildValidatorData({name: 'required', title: t('hotel.hotel.banner')})],
    address: [buildValidatorData({name: 'required', title: t('hotel.hotel.address')})],
    longitude: [buildValidatorData({name: 'required', title: t('hotel.hotel.longitude')})],
    latitude: [buildValidatorData({name: 'required', title: t('hotel.hotel.latitude')})],
    city: [buildValidatorData({name: 'required', title: '所属城市'})],

    hotel_des: [buildValidatorData({name: 'editorRequired', title: t('hotel.hotel.hotel_des')})],
    traffic_bus: [buildValidatorData({name: 'editorRequired', title: t('hotel.hotel.traffic_bus')})],
    book_notice: [buildValidatorData({name: 'editorRequired', title: t('hotel.hotel.book_notice')})],
    create_time: [buildValidatorData({name: 'date', title: t('hotel.hotel.create_time')})],
    update_time: [buildValidatorData({name: 'date', title: t('hotel.hotel.update_time')})],
})
</script>

<style scoped lang="scss"></style>
